package com.md.mymusic

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.living.filedownloader.ui.TasksDownloadedActivity
import com.living.player.ui.recently.RecentlyPlayActivity
import com.md.livingwidget.popwindow.CustomPopWindow
import com.md.mymusic.databinding.FragmentMineBinding
import com.md.mymusic.ui.AboutActivity
import com.yxt.function_sdk_share.constants.SHARE_TYPE
import com.yxt.function_sdk_share.util.ShareUtil

/**
 * @author liyue
 * desc "我听" Fragment
 */
class MyMusicFragment:Fragment() {
    private lateinit var binding:FragmentMineBinding
    private val fragments = ArrayList<Fragment>()
    private lateinit var albumAdapter: AlbumAdapter
    private lateinit var mContext :Context
    private lateinit var  mCustomPopWindow:CustomPopWindow

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = FragmentMineBinding.inflate(LayoutInflater.from(activity))
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mContext= this!!.context!!
        fragments.add(AlbumFragment.getInstance(true))
        fragments.add(AlbumFragment.getInstance(false))
        albumAdapter = AlbumAdapter(activity!!.supportFragmentManager,fragments)
        binding.albumViewPager.adapter=albumAdapter
        binding.smartTab.setViewPager(binding.albumViewPager)

        binding.lltLocalMusic.setOnClickListener {
            startActivity(Intent(activity, TasksDownloadedActivity::class.java))
        }

        binding.lltRecent.setOnClickListener {
            startActivity(Intent(activity, RecentlyPlayActivity::class.java))
        }

        binding.ivSetting.setOnClickListener{
            showPopTopWithDarkBg()
        }

    }

     private fun showPopTopWithDarkBg() {
        val contentView: View = LayoutInflater.from(activity).inflate(R.layout.view_pop_window, null)
         handleLogic(contentView)
         mCustomPopWindow = CustomPopWindow.PopupWindowBuilder(activity)
                .setView(contentView)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .setOnDissmissListener(PopupWindow.OnDismissListener { Log.e("TAG", "onDismiss") })
                .create()
                .showAsDropDown( binding.ivSetting, 0, 20)
    }


    private fun handleLogic(contentView:View ){
        val listener = View.OnClickListener { v ->
            if (mCustomPopWindow != null) {
                mCustomPopWindow.dissmiss()
            }
            when (v.id) {
                R.id.menu1 ->  {activity?.startActivity(Intent(activity,AboutActivity::class.java))}
                R.id.menu2 ->  ShareUtil.shareWithWindows(activity, arrayOf(SHARE_TYPE.WECHAT_FRIENDS,  SHARE_TYPE.COPY), "", "", "", "", null)
            }
        }
        contentView.findViewById<View>(R.id.menu1).setOnClickListener(listener)
        contentView.findViewById<View>(R.id.menu2).setOnClickListener(listener)
    }

   inner class AlbumAdapter: FragmentPagerAdapter {
        private var fragmentList: List<Fragment> = ArrayList()

        constructor(fm: FragmentManager, fragmentList: List<Fragment>):super(fm){
            this.fragmentList=fragmentList
        }

        override fun getItem(position: Int): Fragment {
            return fragmentList[position]
        }

        override fun getCount(): Int {
            return fragmentList.size
        }

        override fun getPageTitle(position: Int): CharSequence? {

            return if (position==0) mContext.resources.getString(R.string.mine_my_album) else mContext.resources.getString(R.string.mine_save_album)
        }

    }


}